//
//  MyAccountInforVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/8.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "MyAccountInforVC.h"
#import "MyAccountInforCell.h"
#import "WalletTiXianVC.h"
#import "TradingRecordVC.h"
#import "CashDepositVC.h"
#import "WalletInfoVC.h"
#import "AccountDetailsVC.h"
#import "TiXianRecordVC.h"
#import "AccountBalanceModel.h"
#import "MyAccountInforCell.h"

@interface MyAccountInforVC ()<UITableViewDelegate,UITableViewDataSource>{
    double _yikaipiao;
    double _weikaipiao;
    double _weishou;
}
@property (strong, nonatomic) CJTableView *tableView;
@property (nonatomic,strong)NSMutableArray *arrTitles;
@property (weak, nonatomic) IBOutlet UIImageView *imgVHeader;
@property (weak, nonatomic) IBOutlet UILabel *labMoney;
@property (nonatomic,copy) NSString *strPingTaiMoney;
@property (nonatomic,strong) NSString *strWangShangMoney;
@property (nonatomic,strong) AccountBalanceModel *model;
@end

@implementation MyAccountInforVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"账户信息")];
    [self setBackLeftButton:@""];
   
    self.imgVHeader.backgroundColor = [UIColor cjMainColor];
    self.strPingTaiMoney = @"";
    self.strWangShangMoney = @"";
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getAccounBalance];
    [self getAccounBalanceInfo];
    [self setWhiterColorAppNavigationBarStyle];
}

/// 获取账户余额
- (void)getAccounBalance{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/statistics/shipmentOrderFeeStatistics",CJBaseUrl);
    NSMutableDictionary *dicPar = [[NSMutableDictionary alloc]init];
    [dicPar setValue:@"1" forKey:@"pageNum"];
    [dicPar setValue:@"20" forKey:@"pageSize"];
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        NSArray *arr = responseObject[@"records"];
        NSDictionary *dic = arr.firstObject;
        _weishou = [dic[@"weishou"] doubleValue];
        _yikaipiao = [dic[@"yikaipiao"] doubleValue];
        _weikaipiao = [dic[@"weikaipiao"] doubleValue];
  
        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];
}
- (void)getAccounBalanceInfo{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/collectionAccountCheck/balance",CJBaseUrl);
    NSDictionary *dicPar = [[NSDictionary alloc]init];
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
         // platformBalance  平台
        // mybankBalance 网商
        
        NSError * error ;
        ws.model = [[AccountBalanceModel alloc]initWithDictionary:responseObject error:&error];
        ws.strPingTaiMoney = @(ws.model.platformBalance).cj_money_description;
        ws.strWangShangMoney = @(ws.model.mybankBalance).cj_money_description;
//        ws.labMoney.text = @(model.platformBalance + model.mybankBalance).cj_money_description;
        CGFloat allMoney = ws.model.platformBalance + ws.model.mybankBalance;
        if(ws.model.staffType == 3){
            ws.labMoney.text = @(ws.model.financeVirtualAmount).cj_money_description;
        }else{
            ws.labMoney.text = @(allMoney).cj_money_description;
        }
       
//        DLog(@"platformBalance = %f mybankBalance = %f 总计是%f",model.platformBalance,model.mybankBalance,model.platformBalance + model.mybankBalance)
        [self getArrTitlesContentAction];
        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];
}

#pragma mark----tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrTitles.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *strTitle = self.arrTitles[indexPath.row];

    static NSString *identifier = @"MyAccountInforCell";
    MyAccountInforCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyAccountInforCell alloc] initWithFlex:@"MyAccountInforCell" reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellInfoWithWithTitle:strTitle myBankBalance:[self.strWangShangMoney isEqualToString:@""] ? @"0":self.strWangShangMoney platformBalance:[self.strPingTaiMoney isEqualToString:@""] ? @"0":self.strPingTaiMoney fenPYe:[NSString stringWithFormat:@"%.2f",self.model.shipmentVirtualAmount] bFenPYe:[NSString stringWithFormat:@"%.2f",self.model.financeVirtualAmount] weishou:_weishou yikaip:_yikaipiao weikaip:_weikaipiao pufaBalance:[@(self.model.pufaBalance).cj_money_description isEqualToString:@""] ? @"0":@(self.model.pufaBalance).cj_money_description hatBalance:[@(self.model.hatBalance).cj_money_description isEqualToString:@""] ? @"0":@(self.model.hatBalance).cj_money_description gdBalance:[@(self.model.gdBalance).cj_money_description isEqualToString:@""] ? @"0":@(self.model.gdBalance).cj_money_description];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *strTitle = self.arrTitles[indexPath.row];
    UIViewController *vc;
    if ([strTitle isEqualToString:@"账户明细"]) {
        vc = [AccountDetailsVC new];
        [self pushVc:vc];
    }else if ([strTitle isEqualToString:@"提现记录"]) {
        vc = [TiXianRecordVC new];
        [self pushVc:vc];
    }else if ([strTitle isEqualToString:@"平台余额"]) {
        return;
    }else if ([strTitle isEqualToString:@"浦发余额"]) {
        WalletInfoVC  *nvc = [WalletInfoVC new];
        nvc.typeStr = @"0";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"浦发提现"]) {
        WalletTiXianVC *nvc = [WalletTiXianVC new];
        nvc.typeStr = @"0";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"快钱余额"]) {
        WalletInfoVC  *nvc = [WalletInfoVC new];
        nvc.typeStr = @"1";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"快钱提现"]) {
        WalletTiXianVC *nvc = [WalletTiXianVC new];
        nvc.typeStr = @"1";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"光大余额"]) {
        WalletInfoVC  *nvc = [WalletInfoVC new];
        nvc.typeStr = @"2";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"光大提现"]) {
        WalletTiXianVC *nvc = [WalletTiXianVC new];
        nvc.typeStr = @"2";
        [self pushVc:nvc];
    }else if ([strTitle isEqualToString:@"交易记录"]) {
        vc = [TradingRecordVC new];
        [self pushVc:vc];
    }else{
        return;
    }

   
    
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, self.imgVHeader.bottom + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  - self.imgVHeader.height - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
    }
    
    return _tableView;
}

- (NSMutableArray *)arrTitles{
    if (!_arrTitles) {
        _arrTitles = [[NSMutableArray alloc]init];
    }
    return _arrTitles;
}

- (void)getArrTitlesContentAction{
    
    [self.arrTitles removeAllObjects];
    
    if ([CJControl getShowMyBank]) {
        [self.arrTitles addObject:@"账户明细"];
    }
    
    if ([CJControl getShowMyBank] || [CJControl getShowAccounWallet]) {
        [self.arrTitles addObject:@"提现记录"];
    }
    [self.arrTitles addObject:@"平台余额"];
    if ([CJControl getShowMyBank] || [CJControl getShowAccounWallet]) {
        [self.arrTitles addObject:@"浦发余额"];
        [self.arrTitles addObject:@"浦发提现"];
        [self.arrTitles addObject:@"快钱余额"];
        [self.arrTitles addObject:@"快钱提现"];
        [self.arrTitles addObject:@"光大余额"];
        [self.arrTitles addObject:@"光大提现"];
    }
    [self.arrTitles addObject:@"交易记录"];
    if(self.model.staffType == 1){
        [self.arrTitles addObject:@"已分配余额"];
        [self.arrTitles addObject:@"待付金额"];
        [self.arrTitles addObject:@"未开票金额"];
        [self.arrTitles addObject:@"已开票金额"];
    }else if(self.model.staffType == 3){
        [self.arrTitles addObject:@"分配余额"];
    }
    
}

@end
